<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编码" prop="districtCode">
        <el-input
          v-model="queryParams.districtCode"
          placeholder="请输入编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="districtName">
        <el-input
          v-model="queryParams.districtName"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="级次" prop="districtLevel">
        <el-input
          v-model="queryParams.districtLevel"
          placeholder="请输入级次"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地区类型" prop="districtType">
        <el-select v-model="queryParams.districtType" placeholder="请选择地区类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_district_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:district:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="districtList"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="父级编号" prop="parentId" />
      <el-table-column label="编码" align="center" prop="districtCode" />
      <el-table-column label="名称" align="center" prop="districtName" />
      <el-table-column label="全称" align="center" prop="fullName" />
      <el-table-column label="显示顺序" align="center" prop="orderNum" />
      <el-table-column label="级次" align="center" prop="districtLevel" />
      <el-table-column label="地区类型" align="center" prop="districtType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_district_type" :value="scope.row.districtType"/>
        </template>
      </el-table-column>
      <el-table-column label="备注信息" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:district:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['base:district:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:district:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改地区对话框 -->
    <el-dialog :title="title" :visible.sync="open" width=600px append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
           <el-col :span=12>
            <el-form-item label="父级编号" prop="parentId">
              <treeselect v-model="form.parentId" :options="districtOptions" :normalizer="normalizer" placeholder="请选择父级编号" />
            </el-form-item>
           </el-col>
           <el-col :span=12>
            <el-form-item label="编码" prop="districtCode">
              <el-input v-model="form.districtCode" placeholder="请输入编码" />
            </el-form-item>
           </el-col>
        </el-row>
        <el-row>
           <el-col :span=12>
            <el-form-item label="名称" prop="districtName">
              <el-input v-model="form.districtName" placeholder="请输入名称" />
            </el-form-item>
           </el-col>
           <el-col :span=12>
            <el-form-item label="全称" prop="fullName">
              <el-input v-model="form.fullName" type="textarea" placeholder="请输入内容" />
            </el-form-item>
           </el-col>
        </el-row>
        <el-row>
           <el-col :span=12>
            <el-form-item label="显示顺序" prop="orderNum">
              <el-input v-model="form.orderNum" placeholder="请输入显示顺序" />
            </el-form-item>
           </el-col>
           <el-col :span=12>
            <el-form-item label="级次" prop="districtLevel">
              <el-input v-model="form.districtLevel" placeholder="请输入级次" />
            </el-form-item>
           </el-col>
        </el-row>
        <el-row>
           <el-col :span=12>
            <el-form-item label="地区类型">
              <el-radio-group v-model="form.districtType">
                <el-radio
                  v-for="dict in dict.type.sys_district_type" :key="dict.value"
:label="dict.value"                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
           </el-col>
           <el-col :span=12>
            <el-form-item label="备注信息" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
           </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm(false)">保 存</el-button>
        <el-button :loading="buttonLoading" type="primary" @click="submitForm(true)">保存并新增</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDistrict, getDistrict, delDistrict, addDistrict, updateDistrict } from "@/api/base/district";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "District",
  dicts: ['sys_district_type'],
  components: {
    Treeselect
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 地区表格数据
      districtList: [],
      // 地区树选项
      districtOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        districtCode: null,
        districtName: null,
        districtLevel: null,
        districtType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "编号不能为空", trigger: "blur" }
        ],
        parentId: [
          { required: true, message: "父级编号不能为空", trigger: "blur" }
        ],
        districtCode: [
          { required: true, message: "编码不能为空", trigger: "blur" }
        ],
        districtName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        fullName: [
          { required: true, message: "全称不能为空", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "显示顺序不能为空", trigger: "blur" }
        ],
        districtLevel: [
          { required: true, message: "级次不能为空", trigger: "blur" }
        ],
        districtType: [
          { required: true, message: "地区类型不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询地区列表 */
    getList() {
      this.loading = true;
      listDistrict(this.queryParams).then(response => {
        this.districtList = this.handleTree(response.data, "id", "parentId");
        this.loading = false;
      });
    },
    /** 转换地区数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.districtName,
        children: node.children
      };
    },
	/** 查询地区下拉树结构 */
    getTreeselect() {
      listDistrict().then(response => {
        this.districtOptions = [];
        const data = { id: 0, districtName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "parentId");
        this.districtOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 保存并新增操作 */
    handleSaveAdd() {
      const parentId = this.form.parentId;
      this.reset();
      this.getTreeselect();
      if (parentId != null) {
          this.form.parentId = parentId;
      } else {
          this.form.parentId = 0;
      }
      this.title = "添加地区";
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        districtCode: null,
        districtName: null,
        fullName: null,
        orderNum: null,
        districtLevel: null,
        districtType: "0",

        coordinate: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        tenantId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.parentId = row.id;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加地区";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
	  this.loading = true;
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.id;
      }
      getDistrict(row.id).then(response => {
	    this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改地区";
      });
    },
    /** 提交按钮 */
    submitForm(isAdd) {
      this.$refs["form"].validate(valid => {
        if (valid) {
		  this.buttonLoading = true;
          if (this.form.id != null) {
            updateDistrict(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              if (isAdd){
                  this.handleSaveAdd();
              }else {
                  this.open = false;
              }
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addDistrict(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              if (isAdd){
                  this.handleSaveAdd();
              }else {
                  this.open = false;
              }
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除地区编号为"' + row.id + '"的数据项？').then(() => {
        this.loading = true;
        return delDistrict(row.id);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    }
  }
};
</script>
